package cn.com.infcn.monitor.server;

import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.infcn.monitor.bean.IFCRequestBean;
import cn.com.infcn.monitor.task.IoTask;
import cn.com.infcn.monitor.task.impl.CpuTask;
import cn.com.infcn.monitor.task.impl.DirectoryTask;
import cn.com.infcn.monitor.task.impl.FileSystemTask;
import cn.com.infcn.monitor.task.impl.JvmTask;
import cn.com.infcn.monitor.task.impl.NetTask;
import cn.com.infcn.monitor.task.impl.OsTask;
import cn.com.infcn.monitor.task.impl.SysMemTask;
import cn.com.infcn.monitor.task.impl.UserTask;
import cn.com.infcn.monitor.task.jvm.impl.JVMClassLoadingTask;
import cn.com.infcn.monitor.task.jvm.impl.JVMCompilationTask;
import cn.com.infcn.monitor.task.jvm.impl.JVMGarbageCollectorTask;
import cn.com.infcn.monitor.task.jvm.impl.JVMMemoryManagerTask;
import cn.com.infcn.monitor.task.jvm.impl.JVMMemoryPoolTask;
import cn.com.infcn.monitor.task.jvm.impl.JVMMemoryTask;
import cn.com.infcn.monitor.task.jvm.impl.JVMOperatingSystemTask;
import cn.com.infcn.monitor.task.jvm.impl.JVMRuntimeTask;
import cn.com.infcn.monitor.task.jvm.impl.JVMThreadTask;
import cn.com.infcn.monitor.util.MonitorType;

/**
 * 
 * @author jijs
 * @Describe
 */
public class IoTaskFactory {

	private final static Logger logger = LoggerFactory
			.getLogger(IoTaskFactory.class);

	private static IoTaskFactory instance;

	public static IoTaskFactory getInstance() {
		if (instance == null) {
			instance = new IoTaskFactory();
		}

		return instance;
	}

	public IoTask createtTask(IoSession session, Object message) {
		IoTask task = null;
		
		try {
			if (message instanceof IFCRequestBean) {
				IFCRequestBean request = (IFCRequestBean)message;
				MonitorType monitorType = request.getMonitorType();
				switch (monitorType){
					case JVM:
						task = new JvmTask(session, message);
						break;
					case CPU:
						task = new CpuTask(session, message);
						break;
					case SYSMEM:
						task = new SysMemTask(session, message);
						break;
					case OS:
						task = new OsTask(session, message);
						break;
					case USER:
						task = new UserTask(session, message);
						break;
					case FILESYSTEM:
						task = new FileSystemTask(session, message);
						break;
					case NET:
						task = new NetTask(session, message);
						break;
					case DIRECTORY:
						task = new DirectoryTask(session, message);
						break;
					case JVMCLASSLOADING:
						task = new JVMClassLoadingTask(session, message);
						break;
					case JVMCOMPILATION:
						task = new JVMCompilationTask(session, message);
						break;
					case JVMGARBAGECOLLECTOR:
						task = new JVMGarbageCollectorTask(session, message);
						break;
					case JVMMEMORYMANAGER:
						task = new JVMMemoryManagerTask(session, message);
						break;
					case JVMMEMORYPOOL:
						task = new JVMMemoryPoolTask(session, message);
						break;
					case JVMMEMORY:
						task = new JVMMemoryTask(session, message);
						break;
					case JVMOPERATINGSYSTEM:
						task = new JVMOperatingSystemTask(session, message);
						break;
					case JVMRUNTIME:
						task = new JVMRuntimeTask(session, message);
						break;
					case JVMTHREAD:
						task = new JVMThreadTask(session, message);
						break;
						
					default :  
				        break;
				}
				logger.warn("I received a unknown request");
			}
		} catch (Exception e) {
		}

		return task;
	}
}
